package com.sz.common.base.utils;

/**
 * Function: CheckDataUtils <br>
 * Author: qiankun<br>
 * Date: 2017-06-05 09:38:00
 */
public class CheckDataUtils {

    public static final String LOWERCASE = ".*[a-z]{1,}.*";
    public static final String UPPERCASE = ".*[A-Z]{1,}.*";
    public static final String NUMBER = ".*\\d{1,}.*";
    public static final String SPECIAL = ".*[~!@#$%^+,()&*/_\\\\.?]{1,}.*";
    public static final String COMPLEX = ".*((?=[\\x21-\\x7e]+)[^A-Za-z0-9]).*";
    public static final int MIN_LEN = 6;
    public static final int MAX_LEN = 18;
    public static final int MIN_COMPLEX = 3;

    /**
     * 验证密码是否满足要求长度6-18 特殊字符等
     */
    public static boolean checkPassword(String password) {
        int checkTypes = 0;
        if (!StringUtils.isEmpty(password)) {
            if (password.length() >= MIN_LEN && password.length() <= MAX_LEN) {
                if (password.matches(LOWERCASE)) {
                    checkTypes++;
                }
                if (password.matches(UPPERCASE)) {
                    checkTypes++;
                }
                if (password.matches(NUMBER)) {
                    checkTypes++;
                }
                if (password.matches(SPECIAL)) {
                    checkTypes++;
                }
                if (checkTypes >= MIN_COMPLEX) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean checkPasswordAnySpecCharacter(String password) {
        int checkTypes = 0;
        if (!StringUtils.isEmpty(password)) {
            if (password.length() >= MIN_LEN && password.length() <= MAX_LEN) {
                if (password.matches(LOWERCASE)) {
                    checkTypes++;
                }
                if (password.matches(UPPERCASE)) {
                    checkTypes++;
                }
                if (password.matches(NUMBER)) {
                    checkTypes++;
                }
                if (password.matches(COMPLEX)) {
                    checkTypes++;
                }
                if (checkTypes >= MIN_COMPLEX) {
                    return true;
                }
            }
        }
        return false;
    }

}
